package com.taysear.parcel.fieldfactory;

import com.taysear.parcel.domain.helpers.Temp;
import com.taysear.parcel.domain.helpers.Temp2;
import com.taysear.parcel.util.Constants;
import com.vaadin.addon.jpacontainer.fieldfactory.FieldFactory;
import com.vaadin.data.Item;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.Field;
import com.vaadin.ui.NativeSelect;
import com.vaadin.ui.TextField;

public class TimeFieldFactory extends FieldFactory {
	
	public final TextField timeId = new TextField("Id");
	final TextField reportingTime = new TextField("Reporting time");
	final TextField departureTime = new TextField("Departure time");
	public NativeSelect amPm = new NativeSelect("Am/Pm");

	@Override
	public Field createField(Item item, Object propertyId, Component uiContext) {
		Field f;
		if ("timeId".equals(propertyId)) {
			timeId.setRequired(true);
			timeId.setWidth(Constants.COMMON_FIELD_WIDTH);
			return timeId;
		} else if ("reportingTime".equals(propertyId)) {
			reportingTime.setRequired(true);
			reportingTime.setWidth(Constants.COMMON_FIELD_WIDTH);
			reportingTime.setRequiredError("Reporting time is required");
			reportingTime.setNullRepresentation("");
			return reportingTime;
		}else if ("reportingTime".equals(propertyId)) {
			departureTime.setRequired(true);
			departureTime.setWidth(Constants.COMMON_FIELD_WIDTH);
			departureTime.setRequiredError("Departure time is required");
			departureTime.setNullRepresentation("");
			return departureTime;
		}else if ("amPm".equals(propertyId)) {
			BeanItemContainer<Temp2> container = new BeanItemContainer<Temp2>(Temp2.class);
			container.addBean(new Temp2("AM", "AM"));
			container.addBean(new Temp2("PM", "PM"));
			amPm.setImmediate(true);
			amPm.setContainerDataSource(container);
			amPm.setNullSelectionAllowed(false);
			amPm.setItemCaptionMode(ComboBox.ITEM_CAPTION_MODE_ITEM);
			amPm.setRequired(true);
			amPm.setRequiredError(amPm.getCaption()+" is required");
			amPm.setWidth(Constants.COMMON_FIELD_WIDTH);
			return amPm;
		} else {
			// Use the super class to create a suitable field base on the
			// property type.
			f = super.createField(item, propertyId, uiContext);
		}
		return f;
	}
}
